package javabase.reflect;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {
	
	public static void main(String[] args) throws ClassNotFoundException {
		String str = "abc";
		Class class1 = str.getClass();
		Class class2 = String.class;
		Class class3 = Class.forName("java.lang.String");
		
		System.out.println(class1 == class2);
		System.out.println(class2.equals(class3));
		
		System.out.println(class1.isPrimitive());
		System.out.println(int.class.isPrimitive());
		
		System.out.println(int.class == Integer.class);
		
		System.out.println(int.class == Integer.TYPE);
		
		System.out.println(int[].class.isPrimitive());
		
		System.out.println(int[].class.isArray());
		
		Constructor constructor;
		try {
			constructor = String.class.getConstructor(StringBuffer.class);
			String string = (String) constructor.newInstance(new StringBuffer("11"));
			System.out.println(string);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Bean bean = new Bean(1L, "dek");
		try {
			Field fieldName = bean.getClass().getField("name");
			System.out.println(fieldName.get(bean));
			
			Field fieldId = bean.getClass().getDeclaredField("id");
			
			fieldId.setAccessible(true);
			System.out.println(fieldId.getLong(bean));
			
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Method methodCharAt = String.class.getMethod("charAt", int.class);
			System.out.println(methodCharAt.invoke(str, 1));
			
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		int[] a1 = new int[3];
		int[] a2 = new int[4];
		int[][] a3 = new int[2][3];
		String[] a4 = new String[3];
		
		System.out.println(a1.getClass() == a2.getClass());
//		System.out.println(a1.getClass() == a3.getClass());
//		System.out.println(a1.getClass() == a4.getClass());
		System.out.println(a1.getClass());
	
		
	}
	
	private static void print(Object obj) {
		Class clazz = obj.getClass();
		if (clazz.isArray()) {
			int lenth = Array.getLength(obj);
			for(int i=0; i<lenth; i++) {
				System.out.print(i);
			}
		} else {
			System.out.println(obj);
		}
	}
	
}
